/*
    Project:        Home Power Control
    File:           HPCThreadedSensor.h
    Author:         christian@karunamusic.at
    Date:           11.2014
    Description:    Baseclass for all sensors with a measure-loop in a thread measuring at a given intervall.
 */

#ifndef HPCTHREADEDSENSOR_H
#define HPCTHREADEDSENSOR_H

#include <boost/thread/thread.hpp>
#include <HPCSensor.h>

/** \brief Baseclass for sensors that requires threaded measurements.
 *  Inherits from HPCSensor.
 */
class HPCThreadedSensor : public HPCSensor
{
public:
    /** \brief Constructor
     *
     * \param id database id of the sensor.
     * \param pin GPIO pin number.
     * \param interval time (in seconds) to pause between querying the sensor.
     *
     */
    HPCThreadedSensor( unsigned long id, unsigned int pin, unsigned int interval );

    /** \brief Destructor.
     */
    virtual ~HPCThreadedSensor();

protected:
    bool m_Run; /**< Thread is running. */
    unsigned int m_Time; /**< pause between measurings in seconds */
    boost::thread *m_thrd; /**< thread handle */

    /** \brief Abstract method to check if sensor has changed (called after interval pause).
     *  Has to be implemented in derived classes!
     *
     * \return true if changed.
     */
    virtual bool hasChanged() = 0;
    /** \brief method to check if sensor is beyond limits.
     *
     * \return int 0 if no alarm, <0 if below limit, >0 if above limit (return index of alam-vector).
     */
    virtual int isAlarm();

private:
    static pthread_mutex_t m_Lock; /**< Lock for mutual exclusion between notifications. */

    /** \brief Thread to run a loop and measure all <m_Time> secs.
     *
     *  Fires an event, if value has changed!
     */
    void mThread();
};

#endif // HPCTHREADEDSENSOR_H
